const Koa = require("koa")
const fs = require('fs')
const path = require('path')
const app = new Koa()

app.use((ctx) => {
    const url = ctx.request.url

    if(url == '/')
    ctx.body = fs.readFileSync('./index.html', 'utf-8')
    else if(url.endsWith('.js')) {
        const p = path.resolve(__dirname,url.slice(1))
        ctx.type = 'text/javascript'
        const source = fs.readFileSync(p.slice,'utf-8')
        ctx.body = rewriteImport(source)
    }else if (url.startsWith("/#modules")) {
        const moduleName =  url.replace("/@modules","")
        const prefix = __dirname + ''

        fs.readFileSync(path.resolve)
    }
    function rewriteImport(sourse) {
        return sourse.replace(/(from\s+['"])(?![\.\/])/g,"$1/@modules/")
    }
}
)

app.listen(8081,()=>{
    console.log("open server localhost:8081")
})
